import 'package:flutter/material.dart';
import 'package:flutter_douban/config/Http_service.dart';
import 'package:flutter_douban/config/app_colors.dart';
import 'package:flutter_douban/main_page/store/view/product_big_commit_cell.dart';
import 'package:flutter_douban/main_page/store/view/product_bottow_cell.dart';
import 'package:flutter_douban/main_page/store/view/product_detail_bottow_view.dart';
import 'package:flutter_douban/main_page/store/view/product_nav_view.dart';
import 'package:flutter_douban/main_page/store/view/product_top_cell.dart';
import 'package:flutter_douban/model/store_category_model.dart';
import 'package:flutter_douban/model/store_product_detail_model.dart';

const APPBAR_SCROLL = 100; //设置滑动变化的偏移量

class StoreProductDetail extends StatefulWidget {
  final StoreCategoryModel model;
  const StoreProductDetail({Key? key, required this.model}) : super(key: key);

  @override
  _StoreProductDetailState createState() => _StoreProductDetailState();
}

class _StoreProductDetailState extends State<StoreProductDetail> {
  /// 滚动控制器
  ScrollController? _controller;
  late StoreProductDetailModel _storeProductDetailModel;
  double appBarAlpha = 0;
  double _offset = 0;

  /// 监听列表偏移
  _onScroll(offset) {
    double alpha = offset / APPBAR_SCROLL;
    if (alpha < 0) {
      alpha = 0;
    } else if (alpha > 1) {
      alpha = 1;
    }
    setState(() {
      appBarAlpha = alpha;
      _offset = offset;
    });
  }

  Future _getListData() async {
    try {
      /// 全部数据
      Map<String, dynamic> result = await HttpService.getMockDataWithPath(
          path: 'mock/store_detail_data.json');
      StoreProductDetailModel storeProductDetailModel =
          StoreProductDetailModel.fromJson(result['result']['p']);
      setState(() {
        _storeProductDetailModel = storeProductDetailModel;
      });

      /// 结束加载状态
    } catch (e) {
      print(e);
    } finally {}
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getListData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Colors.redAccent,
        child: Stack(
          children: <Widget>[
            MediaQuery.removePadding(
              removeTop: true,
              context: context,
              child: NotificationListener(
                onNotification: (scrollNotification) {
                  if (scrollNotification is ScrollUpdateNotification &&
                      scrollNotification.depth == 0) {
                    //滚动且是列表滚动的时候
                    _onScroll(scrollNotification.metrics.pixels);
                  }
                  return true;
                },
                child: Container(
                  color: Colors.white,
                  padding: EdgeInsets.only(
                      top: 0,
                      bottom: MediaQuery.of(context).padding.bottom + 60),
                  child: ListView.builder(
                    // shrinkWrap: true,
                    controller: _controller,
                    itemCount: 3,
                    itemBuilder: (context, index) {
                      if (index == 0) {
                        return ProductTopCell(
                          model: _storeProductDetailModel,
                          storeModel: widget.model,
                        );
                      } else if (index == 1) {
                        return ProductBigCommitCell(
                          list: _storeProductDetailModel.sos!,
                        );
                      } else {
                        return ProductBottowCell(
                          model: _storeProductDetailModel.store!,
                        );
                      }
                    },
                  ),
                ),
              ),
            ),
            ProductNavView(
              appBarAlpha: appBarAlpha,
              offset: _offset,
            ),
            Positioned(
                bottom: MediaQuery.of(context).padding.bottom,
                child: ProductDetailBottowView(
                  model: widget.model,
                ))
          ],
        ),
      ),
    );
  }
}
